using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.ComponentModel;

namespace OdsControllerLib {
	//----------------------------------------------------------------------------------
	// class to hold ods_host records
	[Serializable]
	public class OdsHostsCollectionObj : BindingList<OdsHostObj>, ICloneable {
		string _hostsFile;
		//[XmlArrayItem(ElementName = "Host")]
		//[XmlArray(ElementName = "MyHosts")]
		//List<OdsHostObj> _hosts;

		public string HostsFile {
			get { return _hostsFile; }
			set { _hostsFile = value;}
		}

		public int CountActive {
			get {
				int c = 0;
				for (int i = 0; i < Items.Count; i++) {
					if (Items[i].Active) c++;
				}
				return c;
			}
		}

		public OdsHostsCollectionObj() { }


		public bool LoadFromXML() {
			try {
				if (!File.Exists(_hostsFile))
					throw new Exception("The file doesn't exist. (" + _hostsFile + ")");
				XmlSerializer serializer = new XmlSerializer(typeof(List<OdsHostObj>));
				FileStream fs = new FileStream(_hostsFile, FileMode.Open);
				List<OdsHostObj> loaded_hosts;
				loaded_hosts = (List<OdsHostObj>)serializer.Deserialize(fs);
				fs.Close();
				base.Items.Clear();
				for(int i=0;i<loaded_hosts.Count;i++){
					Items.Add(loaded_hosts[i]);
				}
				return true;
			} catch { return false; }
		}
		public bool SaveToXML() {
			System.Diagnostics.Debug.WriteLine("OdsHostsCollectionObj.SaveToXML - configfile: "+ _hostsFile);
			try {
				if (!Directory.Exists(Path.GetDirectoryName(_hostsFile)))
					throw new Exception("The directory doesn't exist. (" + Path.GetDirectoryName(_hostsFile) + ")");

				XmlSerializer serializer = new XmlSerializer(typeof(List<OdsHostObj>));
				TextWriter writer = new StreamWriter(_hostsFile);
				serializer.Serialize(writer, base.Items);
				writer.Close();
			} catch {
				return false;
			}
			return true;
		}
		//****************************************

		#region ICloneable Members

		public object Clone() {
			MemoryStream buffer = new System.IO.MemoryStream();
			BinaryFormatter formatter = new BinaryFormatter();

			formatter.Serialize(buffer, this);
			buffer.Position = 0;
			return formatter.Deserialize(buffer);
		}
		#endregion ICloneable Members
	}

	//----------------------------------------------------------------------------------
	[Serializable]
	public class OdsHostObj : IDisposable, ICloneable {
		#region Fields
		OdsHostDnsState _dnsstate = OdsHostDnsState.Unknown;
		bool _active = false;
		string _hostName = "";
		string _hostTarget = "";
		string _hostType;
		int _priority = -1;
		int _ttl = -1;
		bool _useConnip = false;
		#endregion Fields

		#region Properties
		[XmlIgnoreAttribute]
		public OdsHostDnsState DnsState {
			get { return _dnsstate; }
			set { _dnsstate = value; }
		}
		public bool Active {
			get { return _active; }
			set { _active = value; }
		}
		public string HostName {
			set { _hostName = value; }
			get { return _hostName; }
		}
		public string HostTarget {
			get { return _hostTarget; }
			set { _hostTarget = value; }
		}
		public string HostType {
			set { _hostType = value.ToUpper(); }
			get { return _hostType; }
		}
		[XmlIgnoreAttribute]
		public int PriorityOrTTL {
			set {
				if (_hostType == "MX") _priority = value;
				else _ttl = value;
			}
			get {
				if (_hostType == "MX") return _priority;
				else return _ttl;
			}
		}
		public int Priority {
			get { return _priority; }
			set { _priority = value; }
		}
		public int TTL {
			get { return _ttl; }
			set { _ttl = value; }
		}
		public bool UseConnip {
			get { return _useConnip; }
			set { _useConnip = value; }
		}
		#endregion Properties


		public bool ValidateRecord() {
			string valerror;
			return ValidateRecord(out valerror);
		}
		public bool ValidateRecord(out string errorMsg) {
			errorMsg = "";
			if (_hostName.Length == 0)
				errorMsg = "Invalid Host Name.";
			if (_hostTarget.Length == 0 & !_useConnip)
				errorMsg = "When not using Automatic IP you must supply a Target IP.";
			if (_hostType.Length == 0 || (_hostType != "AAAA" & _hostType != "A" & _hostType != "NS" & _hostType != "MX" & _hostType != "CNAME"))
				errorMsg = "Invalid Host Type.";
			//if (_hostType == "AAAA" && _ttl <= 0) return false;

			if (errorMsg.Length > 0)
				return false;
			else
				return true;
		}

		#region IDisposable Members

		public void Dispose() {
			this.Dispose();
		}

		#endregion


		#region ICloneable Members

		public object Clone() {
			MemoryStream buffer = new System.IO.MemoryStream();
			BinaryFormatter formatter = new BinaryFormatter();

			formatter.Serialize(buffer, this);
			buffer.Position = 0;
			return formatter.Deserialize(buffer);
		}
		#endregion ICloneable Members


	}

	//----------------------------------------------------------------------------------
}
